package me.w70.bot.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import me.w70.bot.Bot;
import me.w70.bot.account.AccountsGUI;
import me.w70.bot.gui.rs.RSPanel;
import me.w70.bot.scripting.Script;
import me.w70.bot.util.Output;

public class MainWindow implements ActionListener {

	public JFrame frame;

	public static int WAIT_TIME = 50;

	public static int RS_HEIGHT = 500;

	public static int RS_WIDTH = 765;

	public RSPanel rs;

	public static int WORLD = 33;

	public Output output;

	public JButton allowInputBtn = new JButton("Disallow Input");

	public JButton pauseScriptBtn = new JButton("Pause Script");

	private JMenuBar createMenuBar() {
		JMenuBar menuBar;
		JMenu menu, edit, view;
		menuBar = new JMenuBar();
		menu = new JMenu("File");
		edit = new JMenu("Edit");
		view = new JMenu("View");
		menu.addActionListener(this);

		menu.setMnemonic(KeyEvent.VK_F);

		JMenuItem startScriptItem = new JMenuItem("Start Script");
		startScriptItem.addActionListener(this);
		menu.add(startScriptItem);

		JMenuItem stopScriptsItem = new JMenuItem("Stop Scripts");
		stopScriptsItem.addActionListener(this);
		menu.add(stopScriptsItem);

		JMenuItem reloadScriptsItem = new JMenuItem("Reload Scripts");
		reloadScriptsItem.addActionListener(this);
		menu.add(reloadScriptsItem);

		JMenuItem editAccounts = new JMenuItem("Accounts");
		editAccounts.addActionListener(this);
		edit.add(editAccounts);

		JCheckBoxMenuItem viewNPC = new JCheckBoxMenuItem("NPCs");
		viewNPC.addActionListener(this);
		viewNPC.setActionCommand("viewNPCs");

		view.add(viewNPC);

		JCheckBoxMenuItem viewObjects = new JCheckBoxMenuItem("Objects");
		viewObjects.addActionListener(this);
		viewObjects.setActionCommand("viewObjects");
		view.add(viewObjects);

		JCheckBoxMenuItem viewInventory = new JCheckBoxMenuItem("Inventory");
		viewInventory.addActionListener(this);
		viewInventory.setActionCommand("viewInventory");
		view.add(viewInventory);

		menuBar.add(menu);
		menuBar.add(edit);
		menuBar.add(view);

		return menuBar;
	}

	private void createAndShowGUI() {

		frame = new JFrame("W70Bot");

		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		rs = new RSPanel(MainWindow.WORLD, false);

		frame.getContentPane().add(rs, BorderLayout.CENTER);

		/*
		 * JButton b1 = new JButton("screen");
		 * b1.setVerticalTextPosition(AbstractButton.CENTER);
		 * b1.setHorizontalTextPosition(AbstractButton.LEADING);
		 * b1.setMnemonic(KeyEvent.VK_D); b1.setActionCommand("screen");
		 * 
		 * b1.addActionListener(this);
		 * 
		 * frame.getContentPane().add(b1, BorderLayout.WEST);
		 */
		frame.getContentPane().add(Bot.output.scroll, BorderLayout.SOUTH);

		JPanel top = new JPanel();
		top.setLayout(new BoxLayout(top, BoxLayout.X_AXIS));
		allowInputBtn.setActionCommand("disallowInput");
		allowInputBtn.addActionListener(this);

		pauseScriptBtn.setActionCommand("pauseScripts");
		pauseScriptBtn.addActionListener(this);

		top.add(allowInputBtn);
		top.add(pauseScriptBtn);
		frame.getContentPane().add(top, BorderLayout.NORTH);

		// Display the window.
		frame.setLocation(
				(Toolkit.getDefaultToolkit().getScreenSize().width / 2)
						- (MainWindow.RS_WIDTH / 2), (Toolkit
						.getDefaultToolkit().getScreenSize().height / 2)
						- (MainWindow.RS_HEIGHT / 2));
		frame.setJMenuBar(createMenuBar());
		frame.pack();
		frame.setVisible(true);

	}

	boolean run = true;

	public void actionPerformed(ActionEvent e) {
		if ("Stop Scripts".equals(e.getActionCommand())) {
			Bot.stopScripts();
		}

		if ("disallowInput".equals(e.getActionCommand())) {
			allowInputBtn.setText("Allow Input");
			allowInputBtn.setActionCommand("allowInput");
			Bot.allowInput = false;
		}

		if ("viewNPCs".equals(e.getActionCommand())) {
			for (Script s : Bot.getRunningScripts()) {
				if (s.getScriptName().equals("NPC ID")) {
					Bot.scriptLoader.stopScript("NPC ID");
					return;
				}
			}

			for (Script s : Bot.getLoadedScripts()) {
				if (s.getScriptName().equals("NPC ID")) {
					Bot.scriptLoader.runScript("NPC ID");
					return;
				}
			}
			
		}

		if ("viewObjects".equals(e.getActionCommand())) {
			for (Script s : Bot.getRunningScripts()) {
				if (s.getScriptName().equals("Object ID")) {
					Bot.scriptLoader.stopScript("Object ID");
					return;
				}
			}

			for (Script s : Bot.getLoadedScripts()) {
				if (s.getScriptName().equals("Object ID")) {
					Bot.scriptLoader.runScript("Object ID");
					return;
				}
			}
	
		}

		if ("viewInventory".equals(e.getActionCommand())) {
	
			for (Script s : Bot.getRunningScripts()) {
				if (s.getScriptName().equals("Inventory ID")) {
					Bot.scriptLoader.stopScript("Inventory ID");
					return;
				}
			}

			for (Script s : Bot.getLoadedScripts()) {
				if (s.getScriptName().equals("Inventory ID")) {
					Bot.scriptLoader.runScript("Inventory ID");
					return;
				}
			}
		}

		if ("allowInput".equals(e.getActionCommand())) {
			allowInputBtn.setText("Disallow Input");
			allowInputBtn.setActionCommand("disallowInput");
			Bot.allowInput = true;
		}

		if ("pauseScripts".equals(e.getActionCommand())) {
			pauseScriptBtn.setText("Resume Script");
			pauseScriptBtn.setActionCommand("resumeScripts");
			Bot.togglePauseScripts();
		}

		if ("resumeScripts".equals(e.getActionCommand())) {
			pauseScriptBtn.setText("Pause Script");
			pauseScriptBtn.setActionCommand("pauseScripts");
			Bot.togglePauseScripts();
		}

		if ("Accounts".equals(e.getActionCommand())) {
			new AccountsGUI(frame).setVisible(true);
		}

		if ("Reload Scripts".equals(e.getActionCommand())) {
			Bot.reloadScripts();
		}

		if ("Start Script".equals(e.getActionCommand())) {
			if(Bot.getAccounts().size() > 0) {
			new ScriptSelector(frame).setVisible(true);
			} else {
				   JOptionPane.showMessageDialog(null, "Please add an account before selecting a script!");
				   new AccountsGUI(frame).setVisible(true);
			}
		}

	}

	public MainWindow() {

		createAndShowGUI();

	}

}
